package com.sinodata.bsm.cicp.collector.cics;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class TransactionConfigCollector extends Collector {
    private static Logger logger = Logger.getLogger(TransactionConfigCollector.class.getName());

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {

        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader reader1 = null;
        BufferedReader reader2 = null;
        String transName = getParamMap().get("transName");
        String cmfPath = getParamMap().get("cmfPath");
        String cmd1 = "export-tran.sh " + cmfPath + " outfile all";
        String cmd2 = "export-conn.sh " + cmfPath + " outfile all";
        String ptnStr1 = "\\s*(\\S+)\\s+(\\S+)\\s+(\\S*)\\s*";
        String ptnStr2 = "\\s*(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)";

        try {
            reader1 = CommandReader.getReader(ip, cmd1, 0, 0);
            String line = null;
            Pattern p = Pattern.compile(ptnStr1);
            int i = 0;
            String statuString = null;
            Matcher match = null;
            while ((line = reader1.readLine()) != null) {
                if (i != 0) {
                    match = p.matcher(line);
                    if (match.matches()) {
                        if (match.group(1).trim().equals(transName)) {
                            if (match.group(3).trim().equals("")) {
                                statuString = "normal";
                            } else {
                                statuString = match.group(3).trim();
                            }
                            break;
                        }
                    }
                }
                i++;
            }
            reader2 = CommandReader.getReader(ip, cmd2, 0, 0);
            line = null;
            p = Pattern.compile(ptnStr2);
            match = null;
            String startTOD = null;
            String endTOD = null;
            String elapsedTime = null;
            String exrcCICS = null;
            String tdioTime = null;
            String sfsTime = null;
            String tsGet = null;
            String waitCount = null;
            while ((line = reader2.readLine()) != null) {
                match = p.matcher(line);
                if (match.matches()) {
                    if (match.group(1).trim().equals(transName)) {
                        startTOD = match.group(2).trim();
                        endTOD = match.group(3).trim();
                        elapsedTime = timeToSec(match.group(4).trim());
                        exrcCICS = timeToSec(match.group(5).trim());
                        tdioTime = timeToSec(match.group(6).trim());
                        sfsTime = timeToSec(match.group(7).trim());
                        tsGet = match.group(8).trim();
                        waitCount = match.group(9).trim();
                    }
                }
            }
            propValues[0] = ResPropertyValue.createPropValue(statuString, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(startTOD, propMetas[1]);
            propValues[2] = ResPropertyValue.createPropValue(endTOD, propMetas[2]);
            propValues[3] = ResPropertyValue.createPropValue(elapsedTime, propMetas[3]);
            propValues[4] = ResPropertyValue.createPropValue(exrcCICS, propMetas[4]);
            propValues[5] = ResPropertyValue.createPropValue(tdioTime, propMetas[5]);
            propValues[6] = ResPropertyValue.createPropValue(sfsTime, propMetas[6]);
            propValues[7] = ResPropertyValue.createPropValue(tsGet, propMetas[7]);
            propValues[8] = ResPropertyValue.createPropValue(waitCount, propMetas[8]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;

        } catch (IOException e) {
            logger.error("collect CICS status and config status information error", e);
            throw new CollectorException(e);
        } finally {
            if (reader1 != null) {
                try {
                    reader1.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public String timeToSec(String str) {
        Double d = null;
        if (str == null || str.equals("")) {
            return "N/A";
        }
        if (str.matches("\\d+:\\d+\\.\\d+")) {
            String[] timeArr = str.split(":");
            d = Double.parseDouble(timeArr[0]) * 60 + Double.parseDouble(timeArr[1]);
        } else {
            d = Double.parseDouble(str);
        }
        return d.toString();
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "status", "Start-TOD", "End-TOD", "Elapsed-Time", "EXEC-CICS", "TD-IO-Time", "SFS-Time", "TS-GET", "waitCount" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "regionName", "transName", "cmfPath" };
    }

}
